﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StudiesViewModel.cs" company="ELEN 7045 GROUP II">>
//   
// </copyright>
// <summary>
//   The Studies View Model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Presentation.Module.StudyDefinition.ViewModels
{
    #region

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;

    using Presentation.Module.StudyDefinition.Interfaces.Services;
    using Presentation.Module.StudyDefinition.Interfaces.ViewModels;
    using Presentation.Module.StudyDefinition.Interfaces.Views;
    using Presentation.Module.StudyDefinition.Models;

    #endregion

    /// <summary>
    /// The Studies View Model.
    /// </summary>
    public class StudiesViewModel : IStudiesViewModel, INotifyPropertyChanged
    {
        #region Constants and Fields

        /// <summary>
        /// The region manager instance.
        /// </summary>
        private readonly IRegionManager regionManagerInstance;

        /// <summary>
        /// The study add view instance.
        /// </summary>
        private readonly IStudyAddView studyAddViewInstance;

        /// <summary>
        /// The study edit view instance.
        /// </summary>
        private readonly IStudyEditView studyEditViewInstance;

        /// <summary>
        /// The subject groups available for addition.
        /// </summary>
        private ObservableCollection<SubjectGroup> subjectGroupsAvailableForAddition;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StudiesViewModel"/> class.
        /// </summary>
        /// <param name="regionManager">
        /// The region Manager.
        /// </param>
        /// <param name="studiesService">
        /// The studies service.
        /// </param>
        /// <param name="studyAddView">
        /// The study Add View.
        /// </param>
        /// <param name="studyEditView">
        /// The study Edit View.
        /// </param>
        public StudiesViewModel(IRegionManager regionManager, IStudiesUIProxyService studiesService, IStudyAddView studyAddView, IStudyEditView studyEditView)
        {
            this.StudiesServiceInstance = studiesService;
            this.regionManagerInstance = regionManager;

            // Child Views: Add
            this.studyAddViewInstance = studyAddView;
            this.regionManagerInstance.Regions["MainRegion"].Add(studyAddView);

            this.studyEditViewInstance = studyEditView;
            this.regionManagerInstance.Regions["MainRegion"].Add(studyEditView);

            // Wire up the exit event for child views)
            this.studyAddViewInstance.ViewExit += this.ChildViewExit;
            this.studyEditViewInstance.ViewExit += this.ChildViewExit;

            // Setting up command delegates 
            this.EditStudy = new DelegateCommand<Study>(this.OnEditStudy);
            this.AddStudy = new DelegateCommand(this.OnAddStudy);
            this.RemoveStudy = new DelegateCommand<Study>(this.OnRemoveStudy);
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the add study command.
        /// </summary>
        public ICommand AddStudy { get; private set; }

        /// <summary>
        /// Gets the edit study command.
        /// </summary>
        public ICommand EditStudy { get; private set; }

        /// <summary>
        /// Gets the remove study command.
        /// </summary>
        public ICommand RemoveStudy { get; private set; }

        /// <summary>
        /// Gets or sets Studies.
        /// </summary>
        public ObservableCollection<Study> Studies
        {
            get
            {
                return this.StudiesServiceInstance.GetAllStudies();
            }
        }

        /// <summary>
        /// Gets or sets View.
        /// </summary>
        public IStudiesView View { get; set; }

        /// <summary>
        /// Gets A studies service instance
        /// </summary>
        protected IStudiesUIProxyService StudiesServiceInstance { get; private set; }

        /// <summary>
        /// Gets SubjectGroupsAvailableForAddition.
        /// </summary>
        private ObservableCollection<SubjectGroup> SubjectGroupsAvailableForAddition
        {
            get
            {
                return this.StudiesServiceInstance.GetAllSubjectGroups();
            }
        }

        #endregion

        #region Implemented Interfaces

        #region IStudiesViewModel

        /// <summary>
        /// The initialize.
        /// </summary>
        public void Initialize()
        {
            // This is only here for uniformity and future use. Nothing special to initialize on the summary form.
        }

        /// <summary>
        /// Called when [add study].
        /// </summary>
        public void OnAddStudy()
        {
            this.regionManagerInstance.Regions["MainRegion"].Activate(this.studyAddViewInstance);
        }

        /// <summary>
        /// Called when [edit study].
        /// </summary>
        /// <param name="study">
        /// The study.
        /// </param>
        public void OnEditStudy(Study study)
        {
            if (study == null)
            {
                return;
            }

            this.regionManagerInstance.Regions["MainRegion"].Activate(this.studyEditViewInstance);
        }

        /// <summary>
        /// Called when [remove study].
        /// </summary>
        /// <param name="study">
        /// The study.
        /// </param>
        public void OnRemoveStudy(Study study)
        {
            if (study == null)
            {
                return;
            }

            this.StudiesServiceInstance.RemoveStudy(study);
            this.RaiseOnPropertyChanged("Studies");
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Raises the on property changed.
        /// </summary>
        /// <param name="propertyName">
        /// Name of the property.
        /// </param>
        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// The child view exit.
        /// </summary>
        private void ChildViewExit()
        {
            this.regionManagerInstance.Regions["MainRegion"].Activate(this.View);
            this.RaiseOnPropertyChanged("Studies");
        }

        #endregion
    }
}
